//
//  XMLProcess.m
//  TestPhotoMS
//
//  Created by Hung Nguyen Thanh on 10/9/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "XMLProcess.h"
#import "NSData+MBBase64.h"

@implementation XMLProcess

@synthesize XMLDelegate;

- (id)init{
    self = [super init];
    if (self == nil) {
    }
    return self;
}
#pragma mark XML

- (void)parserXMLWithURL:(NSString *)URLStr{
    NSLog(@"URL request: %@", URLStr);
    if (data != nil) {
        data = nil;
    }
    data = [[NSMutableArray alloc] init];
    parser = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:URLStr]];
    parser.delegate = self;
    [parser shouldProcessNamespaces];
    [parser shouldReportNamespacePrefixes];
    [parser parse];
}
- (void)parserDidStartDocument:(NSXMLParser *)parser{
    NSLog(@"Start document");
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError{
    NSLog(@"Error: %@", [parseError localizedDescription]);
}

- (void)parserDidEndDocument:(NSXMLParser *)parser{
    NSLog(@"End document, total photo: %d", [data count]);
    if ([XMLDelegate respondsToSelector:@selector(getCompleteImage:)]) {
        [XMLDelegate getCompleteImage:data];
    }
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
    NSLog(@"Start element: %@", elementName);
    currentElement = elementName;
    if ([elementName isEqualToString:@"entry"]) {
        currentPhoto = nil;
        currentPhoto = [[Photo alloc] init];
    }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
    NSLog(@"End element: %@", elementName);
    if ([elementName isEqualToString:@"entry"]) {
        [data addObject:currentPhoto];
        if ([XMLDelegate respondsToSelector:@selector(haveGetNewImage:)]) {
            [XMLDelegate haveGetNewImage:currentPhoto];
        }
        currentPhoto = nil;
        
    } else if ([elementName isEqualToString:@"d:FileName"]) {
//        NSString *imageURL = [GET_PHOTO_THUMBNAIL stringByAppendingString:currentPhoto.fileName];
//        NSLog(@"%@", imageURL);
//        NSURL *url = [NSURL URLWithString:imageURL];
//        NSData *imageData = [NSData dataWithContentsOfURL:url];
//        currentPhoto.thumbImage = [UIImage imageWithData:imageData];
    }
}
- (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock{
    NSLog(@"Found data");
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    if ([currentElement isEqualToString:@"d:Image"]) {
        if (currentImage == nil) {
            currentImage = string;
        } else {
            currentImage = [currentImage stringByAppendingString:string];
        }
    } else if ([currentElement isEqualToString:@"id"]) {
        [currentPhoto setCategory:string];
    } else if ([currentElement isEqualToString:@"title"]) {
        [currentPhoto setTitle:string];
    } else if ([currentElement isEqualToString:@"name"]) {
        //        [currentPhoto setTitle:string];
    } else if ([currentElement isEqualToString:@"link"]) {
        [currentPhoto setLink:string];
    } else if ([currentElement isEqualToString:@"d:Id"]) {
        [currentPhoto setIdLink:string];
    } else if ([currentElement isEqualToString:@"d:FileName"]) {
        [currentPhoto setFileName:string];
    } else if ([currentElement isEqualToString:@"d:Likes"]) {
        [currentPhoto setLike:[string integerValue]];
    } else if ([currentElement isEqualToString:@"d:Dislikes"]) {
        [currentPhoto setDislike:[string integerValue]];
    } else if ([currentElement isEqualToString:@"d:Longitude"]) {
        [currentPhoto setLongitude:string];
    } else if ([currentElement isEqualToString:@"d:Latitude"]) {
        [currentPhoto setLatitude:string];
    }
}
#pragma mark -
@end
